/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.senac.maisconhecimento.servlet.acesso;

import br.senac.maisconhecimento.dao.UsuarioDAO;
import br.senac.maisconhecimento.entidade.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Marcus Lemos
 */
@WebServlet(name = "AutenticarUsuarioServlet", urlPatterns = {"/AutenticarUsuario"})
public class AutenticarUsuarioServlet extends HttpServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        boolean formValido = true;
        String login, senha;

        login = request.getParameter("login");
        if (login != null) {
            if (login.equals("")) {
                formValido = false;
            }
        } else {
            formValido = false;
        }

        senha = request.getParameter("senha");
        if (senha != null) {
            if (senha.equals("")) {
                formValido = false;
            }
        } else {
            formValido = false;
        }

        if (formValido) {
            UsuarioDAO dao = new UsuarioDAO();

            Usuario usuario = dao.consultarPeloLogin(login);

            if (usuario != null) {

                if (usuario.getSenha().equalsIgnoreCase(senha)) {
                    HttpSession sessionRequest = request.getSession();
                    sessionRequest.setAttribute("usuarioLogado", usuario);

                    RequestDispatcher rd = request.getRequestDispatcher("Dashboard");
                    rd.forward(request, response);

                } else {
                    request.setAttribute("mensagemInvalida", "A combinação usuário e senha é invalida!");
                    RequestDispatcher rd = request.getRequestDispatcher("acesso/Login.jsp");
                    rd.forward(request, response);
                }
            } else {
                request.setAttribute("mensagemInvalida", "O login não existe no sistema!");
                RequestDispatcher rd = request.getRequestDispatcher("acesso/Login.jsp");
                rd.forward(request, response);
            }
        } else {
            request.setAttribute("mensagemInvalida", "Login ou Senha devem ser preenchidos!");
            RequestDispatcher rd = request.getRequestDispatcher("acesso/Login.jsp");
            rd.forward(request, response);
        }
    }
}
